package com.angel.controller;
import com.angel.bean.AcupuncturePoint;
import com.angel.bean.Employee;
import com.angel.bean.Otherlog;
import com.angel.exception.StatusException;
import com.angel.service.AcupuncturePointService;
import com.angel.service.OtherlogService;
import com.angel.shiro.SystemAdminAuthorizingRealm;
import com.angel.utils.AdminUserUtils;
import com.angel.utils.LogUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/Acupoint")
public class AcupunctureController {
    @Resource
    private AcupuncturePointService acupuncturePointService;
    @Autowired
    private OtherlogService otherlogService;
    /**
     * 添加穴位
     * @param acupuncturePoint
     * @return
     */
    @RequestMapping("/addAcupoint")
    @ResponseBody
    public Object addAcupoint(AcupuncturePoint acupuncturePoint){
        Map<String, Object> map = new HashMap<>();
        acupuncturePoint.setAcupName(acupuncturePoint.getAcupName().replace(" ",""));
        acupuncturePoint.setAcupInstruct(acupuncturePoint.getAcupInstruct().replace(" ",""));
        acupuncturePoint.setAcupEffect(acupuncturePoint.getAcupEffect().replace(" ",""));
        acupuncturePoint.setState(1);
        acupuncturePoint.setInputDate(new Timestamp(System.currentTimeMillis()));
//        System.out.print("准备添加穴位---id-"+acupuncturePoint.getAcupId()+"---name-"+acupuncturePoint.getAcupName()+"---no-"
//                +acupuncturePoint.getAcupNo()+"---eff-"+acupuncturePoint.getAcupEffect()+"---inst-"+acupuncturePoint.getAcupInstruct()
//                 +"----sta-"+acupuncturePoint.getState()+"-----"
//                 );
        int result=acupuncturePointService.insert(acupuncturePoint);
        Employee employee = AdminUserUtils.getPrincipal().getEmployee();
        String msg ="";
        if (result != 0){
            map.put("data", "true");
           msg =employee.getEmName()+"添加了穴位"+acupuncturePoint.getAcupName()+"添加成功";
        }else {
            map.put("data", "false");
            System.out.print("添加失败");
            msg =employee.getEmName()+"添加了穴位"+acupuncturePoint.getAcupName()+"添加失败";
        }
        LogUtils.InsertLog(employee,otherlogService,msg);

        return map;
    }
    /**
     * 穴位更新
     * @param acupuncturePoint
     */
    @RequestMapping("/updateAcupuncture")
    public Object updateAcupuncture(AcupuncturePoint acupuncturePoint){
        System.out.print("-------------------------更新穴位"+acupuncturePoint.getAcupId()+"的id");
        acupuncturePoint.setAcupName(acupuncturePoint.getAcupName().replace(" ",""));
        acupuncturePoint.setAcupInstruct(acupuncturePoint.getAcupInstruct().replace(" ",""));
        acupuncturePoint.setAcupEffect(acupuncturePoint.getAcupEffect().replace(" ",""));
        acupuncturePoint.setState(1);
        acupuncturePoint.setInputDate(new Timestamp(System.currentTimeMillis()));
        Map<String, Object> map = new HashMap<>();
        int result=acupuncturePointService.update(acupuncturePoint);
        Employee employee = AdminUserUtils.getPrincipal().getEmployee();
        String msg ="";
        if (result!=0){
            msg ="更新穴位"+  acupuncturePoint.getAcupName()+"成功";
            LogUtils.InsertLog(employee,otherlogService,msg);
            return map.put("data","true");
        }else {
            msg ="更新穴位"+  acupuncturePoint.getAcupName()+"失败";
            LogUtils.InsertLog(employee,otherlogService,msg);
            return map.put("data","false");
        }
    }
    /**
     * 删除穴位
     * @param acupuncturePoint
     * @return
     */
    @RequiresPermissions("user")
    @RequestMapping("/deletAcupuncturePoint")
    @ResponseBody
    public Object deletAcupuncturePoint(AcupuncturePoint acupuncturePoint){
        System.out.print("-----执行删除------------"+"id为："+acupuncturePoint.getAcupId());
        acupuncturePoint.setState(0);
        Map<String, Object> map = new HashMap<>();
        int relsut=acupuncturePointService.deletOneId(acupuncturePoint);
        Employee employee = AdminUserUtils.getPrincipal().getEmployee();
        String msg ="";
        if (relsut!=0){
            msg ="删除穴位"+  acupuncturePoint.getAcupName()+"成功";
            LogUtils.InsertLog(employee,otherlogService,msg);
            return map.put("data","true");
        }else {
            msg ="删除穴位"+  acupuncturePoint.getAcupName()+"失败";
            LogUtils.InsertLog(employee,otherlogService,msg);
            return map.put("data","false");
        }
    }
    /**
     * 查询穴位并且分页显示
     * @param model
     * @param posion
     * @return
     * @RequestParam(value="posion", defaultValue="1")让其默认从第一页开始
     * ,required = false
     */
    @RequestMapping("/FindAcupuncturePoint")
    public String findAcupuncturePoint(@RequestParam(value = "posion", defaultValue = "1") String posion,Model model){
        System.out.print("--------------------------------查询穴位");
        int pageNo = 1;
        try {
            pageNo = Integer.valueOf(posion);
        } catch (Exception e) {

        }
        PageHelper.startPage(pageNo, 10);
        //获取
        List<AcupuncturePoint> acupuncturePoint1List= acupuncturePointService.findAll(null);
        //分页
        PageInfo page=new PageInfo(acupuncturePoint1List);
        page.setList(acupuncturePoint1List);
        if (page!=null){
            model.addAttribute("pageInfo",page);
        }
        return "acupoint";
    }
    /**
     * 查询单个穴位
     * @return
     */
    @RequestMapping("/findOnl")
    @ResponseBody
    public Object findOnl(Integer acupId){
        System.out.println("----------------------------查询单个穴位");
        Map<String,Object> map=new HashMap<>();
        AcupuncturePoint acupuncturePoint=new AcupuncturePoint();
        acupuncturePoint.setAcupId(acupId);
        System.out.print(acupuncturePoint.getAcupId()+"--------------------");
        AcupuncturePoint emp = acupuncturePointService.findOne(acupuncturePoint);
        if (emp != null) {
            map.put("emp", emp);
        }else {
            map.put("emp","false");
        }
        return map;
    }
    /**
     * 查询单个穴的编号
     * @return
     */
    @RequestMapping("/findNo")
    @ResponseBody
    public Object findNo(String acupNo){
        acupNo=acupNo.replace(" ","");
        //System.out.println("----------------------------查询单个穴位编号"+acupNo);
        Map<String,Object> map=new HashMap<>();
        AcupuncturePoint acupuncturePoint=new AcupuncturePoint();
        acupuncturePoint.setAcupNo(acupNo);
        try {
            AcupuncturePoint result= acupuncturePointService.findOne(acupuncturePoint);
            //System.out.print(result.getAcupNo()+"-------------不可用--------------");
            if (result!=null){
                map.put("emp", "false");
            }else {
                map.put("emp", "true");
            }
        }catch (NullPointerException e){
            //System.out.print("------------可用---------------");

        }
        return map;
    }

    /**
     * 查询单个穴位的名称
     * @return
     */
    @RequestMapping("/findName")
    @ResponseBody
    public Object findName(String acupName){
        acupName=acupName.replace(" ","");
        System.out.println("----------------------------查询单个穴位名称"+acupName);
        Map<String,Object> map=new HashMap<>();
        AcupuncturePoint acupuncturePoint=new AcupuncturePoint();
        acupuncturePoint.setAcupName(acupName);
        try {
            AcupuncturePoint result= acupuncturePointService.findOne(acupuncturePoint);
            //System.out.println(result.getAcupName()+"--------------不可用-------------");
            if (result!=null){
                map.put("emp", "false");
            }else {
                map.put("emp", "true");
            }

        }catch (NullPointerException e){
            //System.out.println("------------可用---------------");

        }
        return map;
    }
    /**
     *启用穴位
     * @return
     */
    @RequestMapping("/startAcup")
    public Object startAcup(Integer acupId,Integer state){
        Map<String,String> map=new HashMap<>();
        System.out.print(acupId+"和"+state);
        AcupuncturePoint acupuncturePoint=new AcupuncturePoint();
        acupuncturePoint.setAcupId(acupId);
        if (state==1){
            acupuncturePoint.setState(0);
        }else {
            acupuncturePoint.setState(1);
        }
        int result=acupuncturePointService.updatestate(acupuncturePoint);
        if (result!=0){
            map.put("emp","true");
        }else {
            map.put("emp","false");
        }
        return map;
   }

    /**
     * 模糊查询
     * @param posion
     * @param type
     * @param content
     * @param model
     * @return
     */
   @RequestMapping("/searchAcup")
   @ExceptionHandler(value = StatusException.class)
   public String searchAcup(@RequestParam(value="posion", defaultValue="1")String posion,
                            @RequestParam(value = "type") String type,
                            @RequestParam(value = "content") String content,
                            Model model){

       content=content.replaceAll(" ","");
       System.out.print(type+"和"+content);
       int pageNo = 1;
       try {
           pageNo = Integer.valueOf(posion);
       } catch (Exception e) {

       }
       PageHelper.startPage(pageNo, 10);
       AcupuncturePoint acupuncturePoint;
       //获取
       List<AcupuncturePoint> acupuncturePoint1List = null;
        switch (type){
            case "acupNo" :
                acupuncturePoint=new AcupuncturePoint();
                acupuncturePoint.setReserve1("acupNo");
                acupuncturePoint.setAcupNo("%"+content+"%");
                acupuncturePoint1List= acupuncturePointService.searchAcupuncture(acupuncturePoint);
                break;
            case "acupName" :
                acupuncturePoint=new AcupuncturePoint();
                acupuncturePoint.setReserve1("acupName");
                acupuncturePoint.setAcupName("%"+content+"%");
                acupuncturePoint1List= acupuncturePointService.searchAcupuncture(acupuncturePoint);
                break;
        }

       //分页
       PageInfo page=new PageInfo(acupuncturePoint1List);
       System.out.print(acupuncturePoint1List.size()+"--------------------------------------------");
       model.addAttribute("pageInfo",page);
       model.addAttribute("type",type);
       model.addAttribute("content",content);
       return "acupoint2";
   }
}
